#include "discard.h"
#include <muduo/base/Logging.h>
#include <boost/bind.hpp>

using namespace muduo;
using namespace muduo::net;

DiscardServer::DiscardServer(EventLoop *loop, const InetAddress &listenAddr) 
	: server_(loop, listenAddr, "DiscardServer") {
	server_.setConnectionCallback(boost::bind(&DiscardServer::OnConnection, this, _1));
	server_.setMessageCallback(boost::bind(&DiscardServer::OnMessage, this, _1, _2, _3));
}

void 
DiscardServer::Start() {
	server_.start();
}

void
DiscardServer::OnConnection(const TcpConnectionPtr &conn) {
	LOG_INFO << "DiscardServer - " << conn->peerAddress().toIpPort() << " -> "
	         << conn->localAddress().toIpPort() << " is "
	         << (conn->connected() ? "UP" : "DOWN");
}

void
DiscardServer::OnMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp time) {
	string msg(buf->retrieveAllAsString());
	LOG_INFO << conn->name() << " discards " << msg.size()
	         << " bytes received at " << time.toString();
}
